﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Maticsoft.Common;

namespace Web.Admin.NavItem
{
    public partial class Item_Modify : BaseCode.BaseAdmin
    {
        private DAL_Tags dalTags = new DAL_Tags();
        private string path = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["titlexmlPath"]);

        protected void Page_Load(object sender, EventArgs e)
        {
            string tid = Request.QueryString["id"].ToString();
            TagList();
            bind(tid);
            ViewState["tid"] = tid;
        }
        public void TagList()
        {
            DataTable list = dalTags.GetTagByType(1);
            for (int i = 0; i < list.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Value = list.Rows[i]["id"].ToString();
                li.Text = list.Rows[i]["Name"].ToString();
                li.Attributes.Add("ToopTip", "'" + list.Rows[i]["id"].ToString() + "'");
                this.cblTaglist.Items.Add(li);
            }
        }

        public void bind(string tid)
        {
            string strpath = path;
            string strname = XMLLib.getXmlValue(strpath, "title", "id", tid, "name");
            string strurl = XMLLib.getXmlValue(strpath, "title", "id", tid, "url");
            string strtypes = XMLLib.getXmlValue(strpath, "title", "id", tid, "typeids");

            this.txtName.Text = strname;
            this.txtUrl.Text = strurl;

            for (int i = 0; i < this.cblTaglist.Items.Count; i++)
            {
                if (strtypes.Contains(this.cblTaglist.Items[i].Value))
                {
                    this.cblTaglist.Items[i].Selected = true;
                }
            }
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            string strid = ViewState["tid"].ToString();
            string strname = this.txtName.Text.Trim();
            string strurl = this.txtUrl.Text.Trim();

            string strtypes = "";
            for (int i = 0; i < this.cblTaglist.Items.Count; i++)
            {
                if (this.cblTaglist.Items[i].Selected == true)
                {
                    strtypes += this.cblTaglist.Items[i].Value + ",";
                }
            }
            strtypes = (strtypes.Trim().Length > 0)
                           ? strtypes.Trim().Substring(0, strtypes.Trim().Length - 1)
                           : strtypes;

            XMLLib.UpdateTitle(strid, path, strname, strurl, strtypes);
            Response.Redirect("Item.aspx");
        }
    }
}